/*
 * @Author: Bo
 * @Date: 2022-03-23 11:09:28
 */
import axios, { AxiosInstance } from 'axios'
export class Interceptors {
  instance: AxiosInstance
  constructor() {
    this.instance = axios.create({
      baseURL: '/api',
      timeout: 10000
    })
  }
  // 初始化拦截器
  init() {
    // 请求接口拦截器
    this.instance.interceptors.request.use(
      (config: any) => {
        const token = localStorage.getItem('_token')
        if (token) {
          config.headers['authorization '] = `Bearer ${token}`
        }
        return config
      },
      (err) => {
        console.error(err)
      }
    )

    // 响应拦截器
    this.instance.interceptors.response.use(
      (response) => {
        const res = response.data
        if (!response.status.toString().startsWith('2') || res.code === -1) {
          // 如果状态码不是2开头或者接口code返回-1 则是返回错误信息
          console.error('系统错误，请检查API是否正常！')
          return
        }
        if (res.code !== 0) {
          if (res.code === -3) {
            console.error('登录过期')
          } else {
            if (res.msg) {
              console.error(res.msg)
            }
          }
          return Promise.resolve(res)
        } else {
          // 返回成功则把token存储一下
          // const headers = response.headers;
          // const token = headers.token;
          // const refresh_token = headers["refresh-token"];
          return res
        }
      },
      (error) => {
        if (error.message === 'Request failed with status code 500') {
          console.error('系统错误，请检查API是否正常！')
          return
        }
        let code = -110
        if (error && error.response && error.response.status) {
          code = error.response.status
          // 登陆过期
          // if (code === 401 || code === -3) {
          // }
        } else {
          console.error(error.message)
        }
        const err = { errCode: -110, errMsg: error.message || 'Error' }
        return Promise.resolve(err)
      }
    )
  }
  // 返回一下
  getInterceptors() {
    return this.instance
  }
}
